using MultiAgentsShared;
using static MultiAgentsClient.Manager.WorkflowManager;
using MultiAgentsServer.Scripts.Shared;
using MultiAgentsClient.Presentation.Component;
using Microsoft.UI.Xaml.Controls.Primitives;
using static MultiAgentsClient.Manager.LocalDataOperator;

namespace MultiAgentsClient.Presentation;

public sealed partial class MainPage : Page
{
    private readonly IEventBus eventBus;
    private readonly Dictionary<string, UIElement> componentCache = [];

    public MainPage()
    {
        InitializeComponent();

        DataContext = new BindableMainPageModel();
        eventBus = EventBus.Instance;

        componentCache["ChatRoomList"] = new ChatRoomList();
        componentCache["WorkflowList"] = new WorkflowList();
        componentCache["AgentList"] = new AgentList();
        componentCache["SettingsList"] = new SettingsList();

        componentCache["ChatMainArea"] = new ChatMainArea();
        componentCache["WorkflowWebView"] = new WorkflowWebview();
        componentCache["AgentMainPanel"] = new AgentMainPanel();
        componentCache["SettingsMainArea"] = new SettingsMainArea();

        ListComponentContainer.Content = componentCache["ChatRoomList"];
        RightMainComponentContainer.Content = componentCache["ChatMainArea"];

        NavigationPanel.NavigationToChatRequested += OnNavigationToChatRequested;
        NavigationPanel.NavigationToWorkflowRequested += OnNavigationToWorkflowRequested;
        NavigationPanel.NavigationToAgentManagementRequested += OnNavigationToAgentManagementRequested;
        NavigationPanel.NavigationToUserSettinsRequested += OnNavigationToUserSettinsRequested;
    }

    private void OnNavigationToChatRequested(object? _, EventArgs e)
    {
        ListComponentContainer.Content = componentCache["ChatRoomList"];
        RightMainComponentContainer.Content = componentCache["ChatMainArea"];
    }

    private void OnNavigationToWorkflowRequested(object? _, EventArgs e)
    {
        ListComponentContainer.Content = componentCache["WorkflowList"];
        RightMainComponentContainer.Content = componentCache["WorkflowWebView"];

        eventBus.Publish(new GetAllWorkflowCommand(null));
        ((WorkflowWebview)componentCache["WorkflowWebView"]).LoadWebviewPage(FileConsts.WorkflowPageFileName);
        string jsCode = $"window.updateNodeConfigs({WorkflowNodeConfigToJson.ConvertAllNodeConfigsToJson()});";
        ((WorkflowWebview)componentCache["WorkflowWebView"]).ExecuteJavaScript(jsCode);
    }

    private void OnNavigationToAgentManagementRequested(object? _, EventArgs e)
    {
        ListComponentContainer.Content = componentCache["AgentList"];
        RightMainComponentContainer.Content = componentCache["AgentMainPanel"];
    }

    private void OnNavigationToUserSettinsRequested(object? _, EventArgs e)
    {
        ListComponentContainer.Content = componentCache["SettingsList"];
        RightMainComponentContainer.Content = componentCache["SettingsMainArea"];
        eventBus.Publish(new GetUserSettingsCommand());
    }

    private void Thumb_DragDelta1(object sender, DragDeltaEventArgs e)
    {
        double totalWidth = MainGrid.ActualWidth;
        double row0Width = MainGrid.ColumnDefinitions[0].ActualWidth;
        double row2Width = MainGrid.ColumnDefinitions[2].ActualWidth;
        double row4Width = MainGrid.ColumnDefinitions[4].ActualWidth;

        double newRow0Width = row0Width + e.HorizontalChange;
        double newRow2Width = row2Width - e.HorizontalChange;

        if (newRow0Width > 0 && newRow2Width > 0 && row4Width > 0)
        {
            double row0WidthRatio = newRow0Width / totalWidth;
            double row2WidthRatio = newRow2Width / totalWidth;
            double row4WidthRatio = row4Width / totalWidth;

            MainGrid.ColumnDefinitions[0].Width = new GridLength(row0WidthRatio, GridUnitType.Star);
            MainGrid.ColumnDefinitions[2].Width = new GridLength(row2WidthRatio, GridUnitType.Star);
            MainGrid.ColumnDefinitions[4].Width = new GridLength(row4WidthRatio, GridUnitType.Star);
        }
    }

    private void Thumb_DragDelta2(object sender, DragDeltaEventArgs e)
    {
        double totalWidth = MainGrid.ActualWidth;
        double row0Width = MainGrid.ColumnDefinitions[0].ActualWidth;
        double row2Width = MainGrid.ColumnDefinitions[2].ActualWidth;
        double row4Width = MainGrid.ColumnDefinitions[4].ActualWidth;

        double newRow2Width = row2Width + e.HorizontalChange;
        double newRow4Width = row4Width - e.HorizontalChange;

        if (row0Width > 0 && newRow2Width > 0 && newRow4Width > 0)
        {
            double row0WidthRatio = row0Width / totalWidth;
            double row2WidthRatio = newRow2Width / totalWidth;
            double row4WidthRatio = newRow4Width / totalWidth;

            MainGrid.ColumnDefinitions[0].Width = new GridLength(row0WidthRatio, GridUnitType.Star);
            MainGrid.ColumnDefinitions[2].Width = new GridLength(row2WidthRatio, GridUnitType.Star);
            MainGrid.ColumnDefinitions[4].Width = new GridLength(row4WidthRatio, GridUnitType.Star);
        }
    }
}
